package cn.lj.controller;

import cn.lj.pojo.Blog;
import cn.lj.pojo.Tag;
import cn.lj.service.BlogService;
import cn.lj.service.TagService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author lj
 * @Description:
 * @date 2020-11-29 13:34
 * @QQ 851477108
 */

@Controller
public class TagShowController {

    @Autowired
    private TagService tagService;

    @Autowired
    private BlogService blogService;

    /**
     * 标签展示博客
     * @param id
     * @param pagenum
     * @param model
     * @return
     */
    @GetMapping("/tags/{id}")
    public String types(@PathVariable Long id, @RequestParam(required = false,defaultValue = "1",value = "pagenum")int pagenum, Model model){
        PageHelper.startPage(pagenum, 8);

        List<Tag> tags = tagService.getBlogTag();
        if (id == -1){
            id = tags.get(0).getId();
        }
        List<Blog> blogs = blogService.getBlogByTypeId(id);

        PageInfo<Blog> pageInfo = new PageInfo<>(blogs);
        model.addAttribute("tags",tags);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("activeTagId",id);

        return "tags";
    }
}
